package com.example.regularexpression;


import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * java 正则表达式
 */


public class expression {

    public static void main(String[] args) {
        //按指定模式在字符串查找
        String line = "This order 你好呀 was placed for QT3000! OK?";
        String pattern = "(\\D*)(\\d+)(.*)";
        //创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
        //现在创建matcher对象
        Matcher m = r.matcher(line);
        if (m.find()){
            System.out.println("Found value: "+m.group(0));
            System.out.println("Found value: "+m.group(1));
            System.out.println("Found value: "+m.group(2));
            System.out.println("Found value: "+m.group(3));
        }else {
            System.out.println("NO MATCH");
        }


        System.out.println("\\");    //输出为 \
        System.out.println("\\\\");  //输出为 \\

        String content = "I am noob " +
                "from runoob.com.";
        String pattern2 = ".*runoob.*";
        boolean matches = Pattern.matches(pattern2,content);
        System.out.println("字符串中是否包含了 'runoob' 子字符串? " + matches);

/**
 *       下面是一个对单词 "cat" 出现在输入字符串中出现次数进行计数的例子
 */
         String REGEX = "\\bcat\\b";
         String INPUT = "cat cat cat cattie cat";
        Pattern p = Pattern.compile(REGEX); //获取pattern对象
        Matcher M = p.matcher(INPUT); //获取 matcher对象
        int count = 0;
        while (M.find()){
            count++;
            System.out.println("Match number" +count);
            System.out.println("start();" + M.start());//返回以前匹配的初始索引。
            System.out.println("end();"  + M.end());//返回最后匹配字符之后的偏移量
        }




    }
}
